﻿using Kinlo.MESDocking;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UIWindows.GlobalStatics;

namespace UIWindows.Tools.MesHelper
{
    internal class MesOeeHelper
    {
        static object locked = new object();
        public static void OeeCapacity(CancellationToken IsCancellationRequested, TemporaryStatic _temporaryStatic,
            ParameterStatic _parameterStatic,
            Action<string, string, string, string, string, long, DateTime, string, string, string, string> log, Action<string, string, string> dialog)
        {
            Task.Run(() =>
            {
                var interaction = new OeeCapacityInterface(log, dialog);
                Dictionary<string, object> param = new Dictionary<string, object>
                    {
                        { "machineNo", _parameterStatic.CollectionItemsParameter.EquipNum },
                        { "producttype", _parameterStatic.CollectionItemsParameter.ProductType.ToString() },
                    };
                while (!IsCancellationRequested.IsCancellationRequested)
                {
                    lock (locked)
                    {
                        _temporaryStatic.MinuteProduction = 0;
                    }
                    Thread.Sleep(60 * 1000); //等待一分钟                   
                    Dictionary<string, object> parameter = new Dictionary<string, object>()
                    {
                        { "tenantID", _parameterStatic.CollectionItemsParameter.ProductionLineNo },//租户ID,//产线编号
                        { "equipCode", _parameterStatic.CollectionItemsParameter.EquipNum },//设备编号
                        { "operateId", _temporaryStatic.Mes_Account},//操作者ID
                        { "singleBeat", ""},//单个产品节拍值（浮点值）
                        { "minutePPM",  _temporaryStatic.MinuteProduction.ToString() },//1分钟节拍值（浮点值））                        
                    };

                    param.Add("parameter", parameter);
                    try
                    {
                        interaction.Interaction(_parameterStatic.MESParameter.BaseUrl + _parameterStatic.MESParameter.OeeCapacityRouter,
                            _parameterStatic.MESParameter.OeeCapacityTime,
                            _parameterStatic.MESParameter.OeeCapacityNumber,
                            DateTime.Now, null, param);
                    }
                    catch (Exception)
                    {
                    }
                    param.Remove("parameter");
                }
            }, IsCancellationRequested);

        }

        public static void OeeWarning(UIWindows.Models.PlcAlarmDataModel plcAlarm, TemporaryStatic _temporaryStatic,
          ParameterStatic _parameterStatic,
          Action<string, string, string, string, string, long, DateTime, string, string, string, string> log,
          Action<string, string, string> dialog)
        {
            Task.Run(() =>
            {
                var interaction = new OeeWarningInterface(log, dialog);
                Dictionary<string, object> param = new Dictionary<string, object>
                {
                    { "machineNo", _parameterStatic.CollectionItemsParameter.EquipNum },
                    { "producttype", _parameterStatic.CollectionItemsParameter.ProductType.ToString() },
                };
                Dictionary<string, object> parameter = new Dictionary<string, object>()
                {
                    { "tenantID", _parameterStatic.CollectionItemsParameter.ProductionLineNo },//租户ID,//产线编号
                    { "equipCode", _parameterStatic.CollectionItemsParameter.EquipNum },//设备编号
                    { "operateId", _temporaryStatic.Mes_Account},//操作者ID
                    { "warningCode", plcAlarm.Code},//报警码（需给出导致停机的报警码列表（报警码：报警内容））
                    { "warningSerial", plcAlarm.Id.ToString()},//报警序列号（每次上报唯一的UUID）                 
                };
                if (plcAlarm.IsHasAlarm)//新报警
                {
                    parameter.Add("warningStartTime", plcAlarm.StartTime.ToString("yyyy-MM-dd HH:mm:ss"));//报警开始时间    
                }
                else //结束报警
                {
                    parameter.Add("warningEndTime", plcAlarm.EndTime.ToString("yyyy-MM-dd HH:mm:ss"));//报警结束时间    
                    parameter.Add("responseTime", (int)(plcAlarm.StartTime - plcAlarm.EndTime).TotalSeconds);//报警相应时间     
                }
                param.Add("parameter", parameter);
                try
                {
                    interaction.Interaction(_parameterStatic.MESParameter.BaseUrl + _parameterStatic.MESParameter.OeeWarningRouter,
                        _parameterStatic.MESParameter.OeeWarningTime,
                        _parameterStatic.MESParameter.OeeWarningNumber,
                        DateTime.Now, null, param);
                }
                catch (Exception)
                {
                }
            });
        }
    }
}
